<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <!-- appendChild   innerHTML -->
    <select class="province">
        <option value="">请选择省份</option>
        <!-- <option value="A001">北京</option>
        <option value="A002">上海</option>
        <option value="A003">湖北</option>
        <option value="A004">湖南</option> -->
    </select>
    <select class="city" name="" id="">
        <option value="">请选择市/区</option>
        <!-- <option value="A001">朝阳</option>
        <option value="A002">海淀</option>
        <option value="A003">沙河</option>
        <option value="A004">上地</option> -->
    </select>
    <select class="city" name="" id="" style="display: inline-block;">
        <option value="">请选择市/区</option>
        <!-- <option value="A001">朝阳</option>
        <option value="A002">海淀</option>
        <option value="A003">沙河</option>
        <option value="A004">上地</option> -->
    </select>

</body>
<script>
    // 1. 页面加载时 根据数据生成省份
    // 2. 省份切换时, 通过当前省份id找到对应的省份数据 => 得到该省下市区集合 => 动态生成市区
    // 3. select的html 结构 也是用 innerHTML    
    //      value 用于获取被选中的option的值

    var proSel = document.getElementsByClassName("province")[0];
    var citySel = document.getElementsByClassName("city")[0];

    var xhr = new XMLHttpRequest();
    // xhr.readyState       // 0

    xhr.open("get", "../data/pro.json", true);  // 1

    xhr.send();  // 1

    xhr.onreadystatechange = function () {
        //  readyState  2 3 4  
        //  status      100  200  300  404  500

        if (xhr.readyState == 4 && xhr.status == 200) {
            var result = xhr.responseText;
            // console.log(result);
            var data = JSON.parse(result);
            console.log(data);

            var { list: proList } = data;

            var html = '<option value="">请选择省份</option>';
            // proList.forEach((item) => {
            //     var { city_id, city_name } = item;
            //     html += `<option value="${city_id}">${city_name}</option>`
            // })
            // proList.forEach(({ city_id, city_name }) => { // 函数的参数解构
            //     html += `<option value="${city_id}">${city_name}</option>`
            // })
            proList.forEach(({ city_id, city_name }) => html += `<option value="${city_id}">${city_name}</option>`);
            proSel.innerHTML = html;

            // 数据请求成功之后 在绑定事件
            proSel.onchange = function () {
                var cityId = this.value;
                console.log(cityId);
                if (cityId) {// 有值 => 选择了省份
                    // console.log(proList);
                    // filter  从数组中筛选出满足条件的元素放到数组中  =>新数组
                    var thisPro = proList.filter(({ city_id }) => city_id == cityId)[0];

                    // find    从数组中找到第一个满足条件的元素   => 存在:元素  否则:undefined
                    var thisPro = proList.find(({ city_id }) => city_id == cityId);
                    console.log(thisPro);

                    var { list: cityList } = thisPro;
                    console.log(cityList);

                    var html = '<option value="">请选择市/区</option>';
                    // proList.forEach((item) => {
                    //     var { city_id, city_name } = item;
                    //     html += `<option value="${city_id}">${city_name}</option>`
                    // })
                    // proList.forEach(({ city_id, city_name }) => { // 函数的参数解构
                    //     html += `<option value="${city_id}">${city_name}</option>`
                    // })
                    cityList.forEach(({ city_id, city_name }) => html += `<option value="${city_id}">${city_name}</option>`);
                    citySel.innerHTML = html;


                } else { // 没有值  => 请选择省份  
                    citySel.innerHTML = '<option value="">请选择市/区</option>';
                }
            }
        }
    }



</script>

</html>